package com.qili.controller.base;

import com.qili.base.controller.BaseController;
import com.qili.entity.base.BaseDict;
import com.qili.entity.base.BaseDicttype;
import com.qili.service.base.BaseDictService;
import com.qili.service.base.BaseDicttypeService;
import com.qili.util.JsonUtil;
import com.qili.util.ReType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@Slf4j
@RequestMapping("/sys/dicttype")
public class BaseDicttypeController extends BaseController {
    @Autowired
    BaseDicttypeService baseDicttypeServiceService;

    @Autowired
    private BaseDictService baseDictService;

    @ApiOperation(value = "/index", httpMethod = "GET", notes = "显示字典设置页面")
    @GetMapping("/index")
    public String index(Model model){
        //获取所有的字典类型：


        return "/dict/dictManager";
    }

    @ApiOperation(value = "/list", httpMethod = "GET", notes = "获取字典类型列表")
    @ResponseBody
    @RequestMapping("/list")
    public ReType selectDictTypeList(String dictTypename){
        ReType reType = new ReType();
        if(StringUtils.isNotBlank(dictTypename)){
            Example dictTypeNameExample = new Example(BaseDicttype.class);
            dictTypeNameExample.and().andLike("dictTypename","%"+dictTypename+"%");
            List<BaseDicttype> dicttypeList = baseDicttypeServiceService.selectByExample(dictTypeNameExample);
            reType.setData(dicttypeList);
        }else{
            List<BaseDicttype> dicttypeList = baseDicttypeServiceService.selectAll();
            reType.setData(dicttypeList);
        }
        reType.setCode(0);
        return reType;
    }

    /**
     * 新增字典类型页面
     * @return
     */
    @ApiOperation(value = "/showAdd", httpMethod = "GET", notes = "显示新增字典类型页面")
    @RequestMapping("/showAdd")
    @RequiresPermissions("dictType:add")
    public String showAddType(){

        return "/dict/addDictType";
    }

    /**
     * 字典类型添加
     * @param baseDicttype
     * @return
     */
    @ApiOperation(value = "/addTypeValue", httpMethod = "POST", notes = "增加字典类型")
    @PostMapping(value = "addTypeValue")
    @ResponseBody
    @RequiresPermissions("dictType:add")
    public JsonUtil addType(BaseDicttype baseDicttype) {
        JsonUtil json = new JsonUtil();
        json.setFlag(false);
        if (StringUtils.isBlank(baseDicttype.getDictTypecode()) || StringUtils.isBlank(baseDicttype.getDictTypename())) {
            json.setFlag(false);
            json.setMsg("获取数据失败!!!");
            return json;
        }

        JsonUtil jsonUtil = ifExited(baseDicttype);
        if(!jsonUtil.isFlag()){
            return jsonUtil;
        }

        baseDicttypeServiceService.insertSelective(baseDicttype);
        json.setFlag(true);
        json.setMsg("添加成功");
        return json;
    }

    /**
     * 字典类型编辑页面
     * @param model
     * @param request
     * @param baseDicttype
     * @return
     */
    @ApiOperation(value = "/showUpdateType", httpMethod = "GET", notes = "显示字典类型编辑页面")
    @GetMapping(value = "/showUpdateType")
    @RequiresPermissions("dictType:edit")
    public String UpdateType(Model model, HttpServletRequest request, BaseDicttype baseDicttype) {
        List<BaseDicttype> type = baseDicttypeServiceService.select(baseDicttype);
        if(type.size() > 0){
            model.addAttribute("type",type.get(0));
        }else{
            throw new RuntimeException("未知的字典类型");
        }
        return "/dict/updateDictType";
    }

    /**
     * 编辑字典类型
     * @param baseDicttype
     * @return
     */
    @ApiOperation(value = "/updateTypeValue", httpMethod = "POST", notes = "更新字典类型")
    @PostMapping(value = "/updateTypeValue")
    @ResponseBody
    @RequiresPermissions("dictType:edit")
    public JsonUtil updateTypeValue(BaseDicttype baseDicttype) {
        JsonUtil json = new JsonUtil();
        json.setFlag(false);
        if (baseDicttype == null) {
            json.setFlag(false);
            json.setMsg("获取数据失败!!!");
            return json;
        }
        BaseDicttype old = baseDicttypeServiceService.selectByPrimaryKey(baseDicttype.getDictNo());
        if(!old.getDictTypecode().equals(baseDicttype.getDictTypecode())){
            //说明编码改了
            BaseDicttype sameCodeDictType = new BaseDicttype();
            sameCodeDictType.setDictTypecode(baseDicttype.getDictTypecode());
            JsonUtil jsonUtil = ifExited(sameCodeDictType);
            if(!jsonUtil.isFlag()){
                return jsonUtil;
            }
        }
        if(!old.getDictTypename().equals(baseDicttype.getDictTypename())){
            //说明名称改了
            BaseDicttype sameNameDictType = new BaseDicttype();
            sameNameDictType.setDictTypename(baseDicttype.getDictTypename());
            JsonUtil jsonUtil = ifExited(sameNameDictType);
            if(!jsonUtil.isFlag()){
                return jsonUtil;
            }
        }
        baseDicttypeServiceService.updateByPrimaryKeySelective(baseDicttype);
        json.setFlag(true);
        json.setMsg("操作成功");
        return json;
    }


    /**
     * 判断是否存在
     * @param baseDicttype
     * @return
     */
    public JsonUtil ifExited(BaseDicttype baseDicttype){
        JsonUtil jsonUtil = new JsonUtil();
        if(StringUtils.isNotBlank(baseDicttype.getDictTypecode())){
            BaseDicttype sameCodeDictType = new BaseDicttype();
            sameCodeDictType.setDictTypecode(baseDicttype.getDictTypecode());
            List<BaseDicttype> dbSameCodeDictType = baseDicttypeServiceService.select(sameCodeDictType);
            if(dbSameCodeDictType.size()>0){
                jsonUtil.setFlag(false);
                jsonUtil.setMsg("已经存在的字典类型编号");
                return jsonUtil;
            }
        }
        if(StringUtils.isNotBlank(baseDicttype.getDictTypename())){
            BaseDicttype sameNameDictType = new BaseDicttype();
            sameNameDictType.setDictTypename(baseDicttype.getDictTypename());
            List<BaseDicttype> dbSameNameDictType = baseDicttypeServiceService.select(sameNameDictType);
            if(dbSameNameDictType.size()>0){
                jsonUtil.setFlag(false);
                jsonUtil.setMsg("已经存在的字典类型名称");
                return jsonUtil;
            }
            jsonUtil.setFlag(true);
            return jsonUtil;
        }
        jsonUtil.setFlag(true);
        return jsonUtil;
    }

    /**
     * 删除字典类型
     * @param dictNo
     * @return
     */
    @ApiOperation(value = "/delType", httpMethod = "POST", notes = "删除字典类型")
    @PostMapping(value = "delType")
    @ResponseBody
    @RequiresPermissions("dictType:delete")
    public JsonUtil deleteType(@RequestParam("dictNo") String dictNo) {
        JsonUtil json  = new JsonUtil();
        BaseDicttype dbDictType = baseDicttypeServiceService.selectByPrimaryKey(dictNo);
        if(dbDictType != null && StringUtils.isNotBlank(dbDictType.getDictTypecode())){
            BaseDict delBaseDict = new BaseDict();
            delBaseDict.setBadtTypecode(dbDictType.getDictTypecode());
            baseDictService.delete(delBaseDict);
        }
        baseDicttypeServiceService.deleteByPrimaryKey(dictNo);
        json.setFlag(true);
        return json;
    }
}
